//
//  BarView.m
//  MUBATE
//
//  Created by sunbin on 2017/2/28.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "BarView.h"
#import "YSProgressView.h"


#define roundRadius  10
#define boardWidths  5
#define barWidths    10
#define roundWidths  2*(roundRadius+boardWidths)




@interface BarView()
// 圆的个数
@property (nonatomic ,assign) NSInteger roundCount;
// 比率
@property (nonatomic ,assign) CGFloat rate;
// 目标数据
@property (nonatomic ,strong) NSArray *completeTargetAry;
// 颜色 未完成 完成时颜色
@property (nonatomic ,strong) UIColor *unCompleteBarColor;
@property (nonatomic ,strong) UIColor *completeBarColor;

@property (nonatomic ,assign) barMake barty;



@end


@implementation BarView
{
    CGFloat span;// 间距
    CGFloat lengWidth; //首尾圆间距
    CGFloat ProgressWidth; //进度条长度

    
    CGFloat proportion;//进度条长度/进度条数值大小
}
- (instancetype)init{
    if ([super init]) {
    }
    return self;
}
- (void)reloadData{
    [self layoutSubviews];
    [self setNeedsDisplay];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    

    if ([self.delegate respondsToSelector:@selector(barWithTargetAry)]) {
        self.completeTargetAry = [self.delegate barWithTargetAry];
        self.roundCount = self.completeTargetAry.count;
    }
    
    if ([self.delegate respondsToSelector:@selector(barWithRate)]) {
        self.rate = [self.delegate barWithRate];
    }

    if ([self.delegate respondsToSelector:@selector(completeBarColor)]) {
        self.completeBarColor = [self.delegate completeBarColor];
    }

    if ([self.delegate respondsToSelector:@selector(unCompleteBarColor)]) {
        self.unCompleteBarColor = [self.delegate unCompleteBarColor];
    }

    if ([self.delegate respondsToSelector:@selector(barty)]) {
        self.barty = [self.delegate barty];
        span = self.barty ==barDetai?100:20;
    }


    
}

- (void)drawRect:(CGRect)rect{

    [self setup];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame target:(NSArray *)targetAry rate:(CGFloat)rate barMake:(barMake)barty completeBarColor:(UIColor *)completeBarColor unCompleteBarColor:(UIColor *)unCompleteBarColor{

    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];

        self.rate = rate;
        self.roundCount = targetAry.count;
        self.completeTargetAry = targetAry;
        self.completeBarColor   = completeBarColor;
        self.unCompleteBarColor = unCompleteBarColor;
        span = barty==barDetai?100:20;
        [self setup];

    }
    return self;


}

- (void)setup{
    
    NSArray * labelArray = [self subviews];
    for (UIView * view in labelArray) {
        if ([view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
        if ([view isKindOfClass:[UILabel class]]) {
            [view removeFromSuperview];
        }
        if ([view isKindOfClass:[YSProgressView class]]) {
            [view removeFromSuperview];
        }


    }

    
    // 获取 self大小
    NSLog(@"==%@" ,NSStringFromCGRect(self.frame));

    // 获取总长度
    lengWidth = WIDTH(self) - 2*span;
    
    
    // 获取 圆下部的标签的宽度
    CGFloat labelWidth = (lengWidth - _roundCount*roundWidths)/(_roundCount-1);

    // 获取 进度条长度
    ProgressWidth = WIDTH(self)- 2*span-roundWidths;
    
    // 获取数据源分段数值长度
    CGFloat passage = ([[_completeTargetAry lastObject] floatValue] - [[_completeTargetAry firstObject] floatValue])/(_roundCount-1);
    
    // 满足条件
    CGFloat barW;
    if (_roundCount >1) {
        barW = (ProgressWidth -roundWidths*(_roundCount-1))/(_roundCount-1);
    }
    proportion = barW/passage;//比例
    
    
    // 线处于底部 先画线 区分出来
    YSProgressView*ProgressView = [[YSProgressView alloc] initWithFrame:CGRectMake(span+roundWidths/2, HEIGHT(self)/2-barWidths/2 ,ProgressWidth , barWidths)];

    ProgressView.trackTintColor =_completeBarColor;
    ProgressView.progressTintColor = _unCompleteBarColor;

    
    [self addSubview:ProgressView];

    
    [_completeTargetAry enumerateObjectsUsingBlock:^(NSString *text, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(span+idx*roundWidths+barW*idx, HEIGHT(self)/2-roundWidths/2, roundWidths, roundWidths)];
        [btn setBackgroundColor:[UIColor whiteColor]];
        btn.layer.cornerRadius = roundWidths/2;
        btn.layer.masksToBounds = YES;

        btn.layer.borderWidth = boardWidths;

        // 获取不满足条件上一个index
        NSInteger indexMake;

        if ([text floatValue] <= _rate) {
            
            if ([_completeTargetAry[_roundCount - 1] floatValue] == _rate) {
                // 当完全填充时
                ProgressView.progressValue = ProgressWidth;
            }else{
                indexMake = idx;
            }
            // 边框颜色 完成时的颜色
            btn.layer.borderColor = _completeBarColor.CGColor;
        }else{
        
            // 边框颜色 未完成时的颜色
            btn.layer.borderColor = _unCompleteBarColor.CGColor;

            CGFloat parameterDataLeng = _rate - [[_completeTargetAry objectAtIndex:indexMake] floatValue];

            ProgressView.progressValue =indexMake*barW+indexMake*roundWidths+roundWidths/2 + parameterDataLeng*proportion;
            NSLog(@"proportion=%f" ,proportion);
            NSLog(@"indexMake%ld" ,(long)indexMake);



        }
        
        [self addSubview:btn];
       
        // 创建label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(btn.frame), labelWidth, 20)];
        label.py_centerX = btn.py_centerX;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = FONT_FONTMicrosoftYaHei(8);
        label.text = text;
        [self addSubview:label];
    }];
    
    
    
    
    
    
    
}













@end
